package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.Grade;
import com.xmy.cultivate.entity.GradeDetails;
import com.xmy.cultivate.entity.GradeModule;
import com.xmy.cultivate.entity.GradeOption;
import com.xmy.cultivate.service.IGradeDetailsService;
import com.xmy.cultivate.service.IGradeModuleService;
import com.xmy.cultivate.service.IGradeOptionService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 面试评分详情表 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2024-11-28
 */
@RestController
@RequestMapping("/api/gradeDetails")
public class GradeDetailsController {
    @Autowired
    IGradeDetailsService iGradeDetailsService;

    @Autowired
    IGradeModuleService iGradeModuleService;

    @Autowired
    IGradeOptionService iGradeOptionService;


    @GetMapping("")
    public Result search(@RequestParam(value = "gradeModuleId", defaultValue = "", required = false) String gradeModuleId) {
        // 获取考查内容
        List<GradeDetails> gradeModuleList = iGradeDetailsService.list(new QueryWrapper<GradeDetails>().eq("grade_module_id", gradeModuleId).eq("pid", 0).orderByAsc("sort"));

        for (GradeDetails gradeModule : gradeModuleList) {
            // 获取问题
            List<GradeDetails> gradeDetails = iGradeDetailsService.list(new QueryWrapper<GradeDetails>().eq("pid", gradeModule.getId()).orderByAsc("sort"));
            if (gradeDetails != null && gradeDetails.size() > 0) {
                gradeModule.setGradeDetailsList(gradeDetails);
                for (GradeDetails gradeOption : gradeDetails) {
                    // 获取选项
                    List<GradeOption> gradeOptions = iGradeOptionService.list(new QueryWrapper<GradeOption>().eq("grade_details_id", gradeOption.getId()).orderByAsc("sort"));
                    gradeOption.setGradeOptionList(gradeOptions);
                }
            }
        }

        return new Result(Code.GET_OK, gradeModuleList, Code.GET_OK_MSG);
    }

    @PostMapping("")
    public Result save(@RequestBody GradeDetails gradeDetails) {
        if (gradeDetails.getId() != null) {
            GradeDetails gradeModuleOld = iGradeDetailsService.getById(gradeDetails.getId());
            if (Objects.isNull(gradeModuleOld)) {
                return new Result(Code.EXP_ERROR, null, "该面试评分模块不存在");
            }

            // 先删除选项表的内容，然后再添加
            iGradeOptionService.remove(new QueryWrapper<GradeOption>().eq("grade_details_id", gradeModuleOld.getId()));
        }

        iGradeDetailsService.saveOrUpdate(gradeDetails);

        Long gradeModuleId = gradeDetails.getId();

        if (gradeDetails.getGradeOptionList() != null && gradeDetails.getGradeOptionList().size() > 0) {
            for (GradeOption gradeOption : gradeDetails.getGradeOptionList()) {
                gradeOption.setGradeDetailsId(gradeModuleId);
                iGradeOptionService.saveOrUpdate(gradeOption);
            }
        }

        return new Result(Code.SAVE_OK, null, Code.SAVE_OK_MSG);
    }

    @DeleteMapping("")
    public Result remove(@RequestParam(value = "id") String id) {
        GradeDetails gradeModule = iGradeDetailsService.getById(id);
        if (Objects.isNull(gradeModule)) {
            return new Result(Code.EXP_ERROR, null, "该数据不存在");
        }

        iGradeDetailsService.removeById(id);

        return new Result(Code.DELETE_OK, null, Code.DELETE_OK_MSG);
    }

}
